package cn.cloudtogo.server.handler;

import cn.cloudtogo.server.ErrorWebResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.sql.SQLTransientConnectionException;

@RestControllerAdvice
@Order(0)
@Slf4j
public class SqlExceptionHandler {

    @ExceptionHandler(value = SQLTransientConnectionException.class)
    @ResponseStatus(value = HttpStatus.REQUEST_TIMEOUT)
    public ResponseEntity<ErrorWebResult> handle(SQLTransientConnectionException ex, HttpServletRequest request) {
        log.error("{}", ex.getMessage(), ex);
        ErrorWebResult error = ErrorWebResult.error(HttpStatus.REQUEST_TIMEOUT, request.getRequestURI(), "数据库连接异常");
        return ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT).body(error);
    }

    @ExceptionHandler(value = CannotGetJdbcConnectionException.class)
    @ResponseStatus(value = HttpStatus.REQUEST_TIMEOUT)
    public ResponseEntity<ErrorWebResult> handle(CannotGetJdbcConnectionException ex, HttpServletRequest request) {
        log.error("{}", ex.getMessage(), ex);
        ErrorWebResult error = ErrorWebResult.error(HttpStatus.REQUEST_TIMEOUT, request.getRequestURI(), "中间件连接异常");
        return ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT).body(error);
    }

    @ExceptionHandler(value = SQLSyntaxErrorException.class)
    @ResponseStatus(value = HttpStatus.FORBIDDEN)
    public ResponseEntity<ErrorWebResult> handle(SQLSyntaxErrorException ex, HttpServletRequest request) {
        log.error("{}", ex.getMessage(), ex);
        ErrorWebResult error = ErrorWebResult.error(HttpStatus.FORBIDDEN, request.getRequestURI(), ex.getMessage());
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(error);
    }

    @ExceptionHandler(value = IncorrectResultSizeDataAccessException.class)
    @ResponseStatus(value = HttpStatus.FORBIDDEN)
    public ResponseEntity<ErrorWebResult> handle(IncorrectResultSizeDataAccessException ex, HttpServletRequest request) {
        log.error("{}", ex.getMessage(), ex);
        ErrorWebResult error = ErrorWebResult.error(HttpStatus.FORBIDDEN, request.getRequestURI(), ex.getMessage());
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(error);
    }

    @ExceptionHandler(value = SQLException.class)
    @ResponseStatus(value = HttpStatus.FORBIDDEN)
    public ResponseEntity<ErrorWebResult> handle(SQLException ex, HttpServletRequest request) {
        log.error("{}", ex.getMessage(), ex);
        ErrorWebResult error = ErrorWebResult.error(HttpStatus.FORBIDDEN, request.getRequestURI(), ex.getMessage());
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(error);
    }

    @ExceptionHandler(value = DuplicateKeyException.class)
    @ResponseStatus(value = HttpStatus.CONFLICT)
    public ResponseEntity<ErrorWebResult> handle(DuplicateKeyException ex, HttpServletRequest request) {
        log.error("{}", ex.getMessage(), ex);
        ErrorWebResult error = ErrorWebResult.error(HttpStatus.CONFLICT, request.getRequestURI(), ex.getMessage());
        return ResponseEntity.status(HttpStatus.CONFLICT).body(error);
    }

    @ExceptionHandler(value = EmptyResultDataAccessException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public ResponseEntity<ErrorWebResult> handle(EmptyResultDataAccessException ex, HttpServletRequest request) {
        log.error("{}", ex.getMessage(), ex);
        ErrorWebResult error = ErrorWebResult.error(HttpStatus.NOT_FOUND, request.getRequestURI(), ex.getMessage());
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
    }

}
